var controller 		  = require('./test-svr1'),
	RoutingServer     = require('./routingServer'),
	impl              = require('./impl'),
    utility           = require('system/utility'),
	Connection        = require('dataAccess/connection'),
    port              = parseInt(process.argv[2] || '8000'),
	App               = require('system/app'),
	BinarySearchTree  = require('collections/binarySearchTree'),
	RedBlackTree      = require('collections/redBlackTree'),
    routes;

if (port == 8000)
	routes = { '/test2' : impl.test2 };
else
	routes = { '/test' : impl.test };

var settings = {
	port : port,
	servers : [
		{ port : 8000, address : '127.0.0.1' },
		{ port : 8001, address : '127.0.0.1' },
		{ port : 8002, address : '127.0.0.1' },
		{ port : 8003, address : '127.0.0.1' },
		{ port : 8004, address : '127.0.0.1' }
	],
	routes : routes
};

var tree      = new BinarySearchTree();
var timestamp = new Date().getTime();

timestamp = new Date().getTime();

var a = [];

for (var x = 0; x < 10000000; ++x) {
//	tree.add(Math.floor(Math.random() * 1000000) + 1, 'a');
	a[x] = 'a';
}

timestamp = new Date().getTime();

a.splice(0, 0, 'b');

console.log('Insert Time: ', (new Date().getTime()) - timestamp);

//tree.add(1);
//tree.add(2);
//tree.add(3);

timestamp = new Date().getTime();

for (var x = 0; x < 1000000; ++x) {
	tree.add(Math.floor(Math.random() * 1000000) + 1, 'a');
}

console.log('Tree Insert Time: ', (new Date().getTime()) - timestamp);

timestamp = new Date().getTime();

//tree.add('a');

//console.log('Tree Insert Time: ', (new Date().getTime()) - timestamp);


tree.add(169082);
tree.add(394753);
tree.add(691402);
tree.add(118835);
tree.add(185021);
tree.add(423863);
tree.add(64384);
tree.add(406331);
tree.add(10731);
tree.add(11164);
tree.add(892928);
tree.add(466537);
tree.add(381409);


//tree.add('5');
//tree.add('6');
//tree.add('1');
//tree.add('4');
//tree.add('3');
//tree.add('ba');
//tree.add('bbc');
//tree.add('cd');
//tree.add('aa');

//console.log('BST Add Time: ', (new Date().getTime()) - timestamp);

//timestamp = new Date().getTime();

//tree.traverse('b', function(key, value) {
//	var doesStartWith = (key.indexOf('b') === 0);

//	if (doesStartWith) {
//		console.log(key);
//	}

//	return doesStartWith || (key < 'b');
//});

//console.log('Traverse Time: ', (new Date().getTime()) - timestamp);

//tree.remove(4);

//timestamp = new Date().getTime();

//tree.traverse(function(key, value) {
//	console.log(key);
//	return true;
//});

//console.log('Traverse Time: ', (new Date().getTime()) - timestamp);



//timestamp = new Date().getTime();
//console.log('contains 5: ', tree.contains(5));
//console.log('Contains Time: ', (new Date().getTime()) - timestamp);


/*
var routing = new RoutingServer(settings);

routing.on('started', function() {
	console.log('Server started');
});

routing.start();
*/

/*
var app = new App(
	{
		connectionSettings : {
			connections : {
				'' : {
					host: 'localhost',
					port: 1433,
					userName: 'testdev',
					password: 'testdev',
					database: 'Node'
				}
			}
		}
	}
);

app.start(function() {
	console.log('started');
});
*/

/*
connection.connect(function(errs) {
	var stmt = connection.createStatement('SELECTd * FROM [Users]');
	
	stmt.on('row', function(row) {
		console.log('Received row');
	});
	
	stmt.on('done', function(result) {
		console.log(result);
		console.log('isError ', result.isError);
		console.log('hasMore ', result.hasMore);
		console.log('isCanceled ', result.isCanceled);
		console.log('isFatal ', result.isFatal);
		
		if (result.isError) {
			console.log('done error');
		}
		
		connection.commit(function(errs) {
			if (errs != null) {
				console.error(errs);
			} else {
				console.log('Successfully committed');
			}
		});
	});
	
	stmt.on('error', function(a) {
		console.log('error', a);
	});
	
	connection.beginTransaction();
	stmt.execute();
});
*/
